package com.ztjava.core.utiliy;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.xiaoleilu.hutool.date.DateUtil;


public class StringUtil {
	// 过滤特殊字符 
	public static String StringFilter(String str) throws PatternSyntaxException {
		// 清除掉所有特殊字符
		String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	} 
	
	public static String hidePhone(String phone){
		return phone.substring(0,3)+"****"+phone.substring(7,11);
	}
	//根据前缀、年月、ID及五位随机数生成不重复订单号
	public static String orderNum(String str,Integer id){
		String num = str.toUpperCase();
		if(num.length()>2){
			num = str.substring(0,2);
		}
		num = num + DateUtil.format(new Date(),"yyMM");
		String object ="00000"+id;
		Random random = new Random();
		Integer randomInt = random.nextInt(89999)+10000;
		num = num + object.substring(object.length()-5,object.length())+randomInt;
		return num;
	}
	/***
	 * 判断是否全是数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/***
	 * 首字母小写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstLowerCase(String str) {
		char[] chars = new char[1];
		chars[0] = str.charAt(0);
		String temp = new String(chars);
		if (chars[0] >= 'A' && chars[0] <= 'Z') {
			return str.replaceFirst(temp, temp.toLowerCase());
		} else {
			return str;
		}
	}

	/***
	 * ids转字符串
	 * 
	 * @param ids
	 * @return
	 */
	public static String idsToString(String[] ids) {
		if (ids == null)
			return null;
		String str = "";
		for (String id : ids) {
			if (id.trim().equals("")) {
				continue;
			}
			str = str + id + ",";
		}
		if (!str.equals("")) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}
	public static String getRandomString(int length) { //length表示生成字符串的长度
	    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }
	public static String getRandomInt(int length) { //length表示生成字符串的长度
	    String base = "0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }
	
	public static String multiple(String[] selects,String end){
		if(selects==null) return null;
		String str = "";
		for(String select:selects){
			str = str + select + end;
		}
		if(!str.equals("")){
			str = end+str;
		}
		return str;
	}
	public static String multiple(String[] selects){
		return multiple(selects,",");
	}
	//两边没有符号
	public static String multiple2(String[] selects){
		return multiple2(selects,",");
	}
	public static String multiple2(String[] selects,String end){
		if(selects==null) return null;
		String str = "";
		for(String select:selects){
			str = str + select + end;
		}
		if(!str.equals("")){
			str = str.substring(0,str.length()-1);
		}
		return str;
	}
	 /**
	 * 过滤掉超过3个字节的UTF8字符
	 * @param text
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	 public static String filterOffUtf8Mb4(String text) {
		 try{
			 byte[] bytes = text.getBytes("utf-8");
	         ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
	         int i = 0;
	         while (i < bytes.length) {
	             short b = bytes[i];
	             if (b > 0) {
	                 buffer.put(bytes[i++]);
	                 continue;
	             }
	             b += 256; // 去掉符号位
	             if (((b >> 5) ^ 0x6) == 0) {
	                 buffer.put(bytes, i, 2);
	                 i += 2;
	             } else if (((b >> 4) ^ 0xE) == 0) {
	             buffer.put(bytes, i, 3);
	                 i += 3;
	             } else if (((b >> 3) ^ 0x1E) == 0) {
	                 i += 4;
	             } else if (((b >> 2) ^ 0x3E) == 0) {
	                 i += 5;
	             } else if (((b >> 1) ^ 0x7E) == 0) {
	                 i += 6;
	             } else {
	                 buffer.put(bytes[i++]);
	             }
	         }
	         buffer.flip();
	         return new String(buffer.array(), "utf-8");
		 }catch (Exception e) {
			 
		 }
	     return text; 
	} 
	 
		private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
	    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
	    
	    /**
	     * @param htmlStr
	     * @return
	     *  删除Html标签
	     */
	    public static String delHTMLTag(String htmlStr) {
	        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
	        Matcher m_script = p_script.matcher(htmlStr);
	        htmlStr = m_script.replaceAll(""); // 过滤script标签
	 
	        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
	        Matcher m_style = p_style.matcher(htmlStr);
	        htmlStr = m_style.replaceAll(""); // 过滤style标签
	 
	        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
	        Matcher m_html = p_html.matcher(htmlStr);
	        htmlStr = m_html.replaceAll(""); // 过滤html标签
	 
	        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
	        Matcher m_space = p_space.matcher(htmlStr);
	        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
	        return htmlStr.trim().replace(" ", ""); // 返回文本字符串
	    }
}
